//
//  LofterMvpCourseManager.m
//  Pods
//
//  Created by zmm on 8/5/2017.
//
//

#import "LofterMvpCourseManager.h"
#import "LFTMCSessionViewController.h"
#import "UIButton+LFTMCBlock.h"
#import "LFTMCUtil.h"
#import "NTESService.h"

#import "UIView+Toast.h"
#import "NTESService.h"
#import "NTESNotificationCenter.h"
#import "NTESLogManager.h"
#import "NTESDemoConfig.h"
#import "NTESSessionUtil.h"
#import "NTESMainTabController.h"
#import "NTESLoginManager.h"
#import "NTESCustomAttachmentDecoder.h"
#import "NTESClientUtil.h"
#import "NTESNotificationCenter.h"
#import "NIMKit.h"
#import "NTESSDKConfigDelegate.h"
#import "NTESCellLayoutConfig.h"

@interface LofterMvpCourseManager()

@end

@implementation LofterMvpCourseManager


+ (void)showWithDataSource:(id<LofterMvpCourseDataSource>)dataSource
                 groupInfo:(LofterMvpCourseTeamModel *)teamModel
                    fromVC:(UIViewController *)fromVC
            showCompletion:(void (^)(BOOL))completion
{
    // login
    [[NIMSDK sharedSDK].loginManager login:teamModel.accountId token:teamModel.accountToken completion:^(NSError * _Nullable error) {
        [[NTESServiceManager sharedManager] start];
        if (!error) {
            [[NIMSDK sharedSDK].teamManager fetchTeamInfo:teamModel.teamId completion:^(NSError * _Nullable error, NIMTeam * _Nullable team) {
                if (!error) {
                    [[NIMSDK sharedSDK].teamManager fetchTeamMutedMembers:teamModel.teamId completion:^(NSError * _Nullable error, NSArray<NIMTeamMember *> * _Nullable members) {
                        if (!error) {
                            NIMSession *session = [NIMSession session:teamModel.teamId type:NIMSessionTypeTeam];
                            LFTMCSessionViewController *vc = [[LFTMCSessionViewController alloc] initWithSession:session];
                            
                            NSString *customInfoString = team.serverCustomInfo;
                            NSDictionary *customInfo = [LFTMCUtil stringToDictionary:customInfoString];
                            NIMTeamMember *teamMember = [[NIMSDK sharedSDK].teamManager teamMember:teamModel.accountId inTeam:teamModel.teamId];
                            LFTMCUtil *util = [LFTMCUtil sharedInstance];
                            util.teamModel = teamModel;
                            util.dataSource = dataSource;
                            util.lftmcVC = vc;
                            util.mutedMembers = members;
                            util.customInfo = customInfo;
                            util.myTeamInfo = teamMember;
                            
                            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
                            [fromVC presentViewController:nav animated:YES completion:nil];
                            [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
                            
                            [[NIMSDKConfig sharedConfig] setEnabledHttpsForInfo:NO];
                            
                            completion(YES);
                        } else {
                            completion(NO);
                        }
                    }];
                } else {
                    completion(NO);
                }
            }];
        }else{
            completion(NO);
        }
    }];
}

+ (void)registWithAppKey:(NSString *)appKey
{
    [[NIMSDKConfig sharedConfig] setDelegate:[[NTESSDKConfigDelegate alloc] init]];
    [[NIMSDKConfig sharedConfig] setShouldSyncUnreadCount:YES];
    [[NTESNotificationCenter sharedCenter] start];
    [[NIMSDK sharedSDK] registerWithAppID:appKey cerName:nil];
}

+ (void)clearCache
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
//    NSArray *files = [fileManager subpathsAtPath:[paths firstObject]];
    NSError *error = nil;
    NSArray *nimsdkDirs = [fileManager contentsOfDirectoryAtPath:[paths firstObject] error:&error];
    if (error) {
        return;
    }
    NSString *nimsdkpath = [NSString stringWithFormat:@"%@/%@",[paths firstObject], [nimsdkDirs firstObject]];
    NSArray *subnimsdkDirs = [fileManager contentsOfDirectoryAtPath:nimsdkpath error:&error];
    if (error) {
        return;
    }
    
    for (NSString *path in subnimsdkDirs) {
        BOOL isDir = NO;
        NSString *checkPath = [NSString stringWithFormat:@"%@/%@", nimsdkpath, path];
        BOOL existed = [fileManager fileExistsAtPath:checkPath isDirectory:&isDir];
        if (existed && isDir && [path length] > 25) {
//            if (![fileManager removeItemAtPath:path error:&error]) {
//                NSLog(@"Unable to delete file: %@", [error localizedDescription]);
//            }
            [self clearCacheWithFilePath:checkPath];
        }
    }
}

+ (void)clearCacheWithFilePath:(NSString *)path{
    
    //拿到path路径的下一级目录的子文件夹
    NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
    
    NSString *filePath = nil;
    
    NSError *error = nil;
    
    for (NSString *subPath in subPathArr)
    {
        filePath = [path stringByAppendingPathComponent:subPath];
        
        //删除子文件夹
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        
    }
}

@end
